package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.dubbo.container.page.PageHandler;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * @author zhouwei
 * @date 2020-01-16 15:25
 */
@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;

    /**
     * 查询所有菜单
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //调用分页助手
        PageHelper.startPage(currentPage,pageSize);
        Page<Menu> page=menuDao.findPage(queryPageBean);
        long total = page.getTotal();
        List<Menu> result = page.getResult();
        List<Map> list = new ArrayList<>();
        //查询每一个菜单的所属角色,添加到menu中
        for(Menu menu:result){
            Set<Role> roles=menuDao.findRoles(menu.getName());
            menu.setRoles(roles);
            //将roles转换为String
            Map map=new HashMap<>();
            map.put("name",menu.getName());
            map.put("path",menu.getPath());
            map.put("linkUrl",menu.getLinkUrl());
            String rolesName="";
            for(Role role:menu.getRoles()){
                rolesName+=role.getName()+" ";
            }
            map.put("roles",rolesName);
            list.add(map);
        }
        PageResult  pageResult= new PageResult(total,list);
        return pageResult;
    }
}
